package com.xksms.miniotest.utils.text;

import com.xksms.miniotest.exception.ExceptionEnum;

import java.io.Serializable;

/**
 * @ClassName JsonResult
 * @Description 返回结果类
 * @Author 程超
 * @Date 2022/6/14 16:57
 */
public class JsonResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer code;
    private String msg;
    private T data;

    public JsonResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public JsonResult() {
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    private static <T> JsonResult<T> jsonResult(Integer code, String msg, T data){
        JsonResult<T> result = new JsonResult<>();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    public static <T> JsonResult<T> success() {
        return jsonResult(ExceptionEnum.SUCCESS.getCode(),  ExceptionEnum.SUCCESS.getMsg(), null);
    }

    public static  <T> JsonResult<T> success(String msg) {
        return jsonResult(ExceptionEnum.SUCCESS.getCode(),  msg, null);
    }

    public static <T> JsonResult<T> success(String msg, T data) {
        return jsonResult(ExceptionEnum.SUCCESS.getCode(),  msg, data);
    }

    public static   <T> JsonResult<T> success(T data) {
        return  jsonResult(ExceptionEnum.SUCCESS.getCode(),  ExceptionEnum.SUCCESS.getMsg(), data);
    }

    public static JsonResult error() {
        return jsonResult(ExceptionEnum.ERROR.getCode(),  ExceptionEnum.ERROR.getMsg(), null);
    }

    public static JsonResult error(ExceptionEnum exceptionEnum) {
        return jsonResult(exceptionEnum.getCode(),exceptionEnum.getMsg(), null);
    }

    public static JsonResult error(String msg) {
        return jsonResult(ExceptionEnum.ERROR.getCode(),msg, null);
    }


}